//
//  UserApi.m
//  GLuckyTransport
//
//  Created by mac on 15/8/1.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "ModelApi.h"
#import "ModelA.h"

#define kENCRYPTKey @"d985ca5f92e3ed87a71753bbc4fbf5c9"
#define kENCRYPTIv  @"AFDFSDFSDFDSDSFE"


@implementation ModelApi

#pragma mark - base

ON_WebAPI(dlg){
    
    if (dlg.sending) {
        [self callbackListenter:dlg.name bean:nil state:APIProcessLoading userinfo:[dlg webUserinfo] error:nil];
    }else if (dlg.succeed){
        
        NSDictionary *params = [dlg.output objectForKey:[HMWebAPI params]];
        WebAPIModelResult *result = [params objectForClass:[WebAPIModelResult class]];

        if (result.successI) {
            
            Class clazz = [self classForCommand:dlg.command];
            result.resultClass = clazz;
            
            [self callbackListenter:dlg.name bean:result state:APIProcessSucced userinfo:[dlg webUserinfo] error:nil];
            
        }else{
            
            [self callbackListenter:dlg.name bean:result state:APIProcessFailed userinfo:[dlg webUserinfo] error:nil];
        }
        
    }else if (dlg.failed){
        NSInteger state = APIProcessFailed;
        WebAPIModelResult *result = [[WebAPIModelResult alloc]init];
        result.success  = @(0);
        
        if (dlg.timeout) {
            state = APIProcessTimeOut;
            result.message = @"网络连接超时";
            result.error_code = @(-409);
        }else{
            result.message = @"网络连接失败";
            result.error_code = @(-408);
        }
        
        [self callbackListenter:dlg.name bean:result state:state userinfo:[dlg webUserinfo] error:nil];
        
    }else if (dlg.cancelled){
        
    }
}


- (HMDialogue *)postOrGet:(BOOL)ispost paras:(id)dic to:(NSString *)command alias:(NSString *)alias encrypt:(BOOL)encrypt asData:(BOOL)asData form:(id<APICallBackProtocol>)receiver{
    [self observeListener:receiver forCommand:alias];
    
    Class cls = [[ModelA classNames] valueForKey:command];
    
    [self registerClassName:cls forCommand:command];
    
    NSData * encryptedData = dic;
    
    if (asData&&![dic isKindOfClass:[NSData class]]) {
       encryptedData = [[dic isKindOfClass:[NSString class]]?(NSString*)dic:[dic JSONString] dataUsingEncoding:NSUTF8StringEncoding];
    }
    
    if (encrypt&&[encryptedData isKindOfClass:[NSData class]]) {
        encryptedData = [[StringEncryption alloc] encrypt:encryptedData key:kENCRYPTKey iv:kENCRYPTIv];
    }
    
    HMDialogue *dia = [[[self webApiWithCommand:command timeout:20.f] setWebName:alias] setWebMethod:ispost?@"POST":@"GET"];
    
    if ([encryptedData isKindOfClass:[NSData class]]) {
        [dia setWebData:encryptedData];
    }else if ([encryptedData isKindOfClass:[NSDictionary class]]){
        [dia setWebParams:dic];
    }
    
    [dia setWebHeaders:@{@"userid":@"2",@"platform":@"ios",@"app":@"user",@"uuid":[HMSystemInfo deviceUUID]}];
    
//    [dia setWebHeaders:@{@"userid":@"3",@"platform":@"ios",@"app":@"driver",@"uuid":[HMSystemInfo deviceUUID]}];
    
    [dia send];
    return dia;
}

@end
